package com.jinlia.show.modules.wxapp.controller;

import com.jinlia.show.common.core.utils.UserInfoUtils;
import com.jinlia.show.modules.base.system.entity.SysAreaCode;
import com.jinlia.show.modules.base.system.service.AreaCodeMapperService;
import com.jinlia.show.modules.base.model.vo.SysAreaCodeVO;
import com.jinlia.show.modules.base.wxapp.entity.UserAddress;
import com.jinlia.show.modules.base.wxapp.service.UserAddressMapperService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AreaCodeMapperService areaCodeMapperService;
    @Autowired
    private UserAddressMapperService userAddressMapperService;

    @PostMapping(value = "/code")
    public ResponseEntity<SysAreaCodeVO> getListVO(@RequestBody SysAreaCode areaCode) {
        SysAreaCodeVO returnVO = new SysAreaCodeVO();
        List<SysAreaCode> list = areaCodeMapperService.getList(areaCode);
        if (Objects.isNull(areaCode.getCode())) {//查询省份
            returnVO.setCodeType("province");
        } else if (areaCode.getCode() % 10000 == 0) { //查询城市
            returnVO.setCodeType("city");
        } else {//查询地区
            returnVO.setCodeType("area");
        }
        //去除自己
        list = list.stream().filter(i -> !i.getCode().equals(areaCode.getCode())).collect(Collectors.toList());
        returnVO.setAreaCodes(list);
        return ResponseEntity.ok(returnVO);
    }

    @GetMapping("{id}")
    public UserAddress add(@PathVariable Long id) {
        UserAddress byId = userAddressMapperService.getById(id);
        return byId;
    }

    /**
     * 新增或更新
     *
     * @return
     */
    @PostMapping("addOrUpdate")
    public UserAddress addOrUpdate(@RequestBody UserAddress userAddress) {
        userAddress.setUserId(UserInfoUtils.getUserId());
        if (userAddress.getId() == null) {
            userAddressMapperService.save(userAddress);
        } else {
            userAddressMapperService.updateById(userAddress);
        }
        return userAddress;
    }

    /**
     * 微信导入的地址
     *
     * @param userAddress
     * @return
     */
    @PostMapping("import")
    public UserAddress importUserAddress(@RequestBody UserAddress userAddress) {
        userAddress.setUserId(UserInfoUtils.getUserId());
        return userAddressMapperService.importUserAddress(userAddress);
    }

    @PostMapping("list")
    public List<UserAddress> get(@RequestBody UserAddress userAddress) {
        List<UserAddress> list = userAddressMapperService
                .lambdaQuery().eq(UserAddress::getUserId, UserInfoUtils.getUserId())
                .orderByDesc(UserAddress::getLastUpdateTime) //创建时间降序
                .list();
        return list;
    }

    @GetMapping("del/{id}")
    public Boolean getLastOne(@PathVariable("id") Long id) {

        return userAddressMapperService.removeById(id);
    }

    @PostMapping("update")
    public UserAddress update(@RequestBody UserAddress userAddress) {
        userAddressMapperService.updateById(userAddress);
        return userAddress;
    }

    @PostMapping("setDefault")
    public UserAddress setDefault(@RequestBody UserAddress userAddress) {
        userAddressMapperService.lambdaUpdate()
                .set(UserAddress::getIsDefault, 0)
                .eq(UserAddress::getOpenId, "1")
                .update();
        userAddress.setIsDefault(1);
        userAddressMapperService.updateById(userAddress);
        return userAddress;
    }

    @PostMapping("getDefault")
    public UserAddress getDefault(@RequestBody UserAddress userAddress) {
        List<UserAddress> list = userAddressMapperService.lambdaQuery()
//                .eq(UserAddress::getUserId, UserInfoUtils.getUserId())
                .orderByAsc(UserAddress::getCreateTime)
                .list();
        List<UserAddress> defaultList = list.stream().filter(i -> Integer.valueOf(1).equals(i.getIsDefault())).collect(Collectors.toList());
        if (defaultList.size() > 0) {
            return defaultList.get(0);
        } else {
            return list.get(0);
        }
    }
}
